package com.shop2cn.iapi.productprice.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.shop2cn.iapi.productprice.parameter.GetMchCatalogPriceListBean;
import com.shop2cn.iapi.productprice.service.GetMchCatalogPriceListCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;
/**
*商家采购价格列表(ByCatalogId)Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetMchCatalogPriceList{
	private static GetMchCatalogPriceListBean getmchcatalogpricelistBean;
	private static GetMchCatalogPriceListCall getmchcatalogpricelistCall;
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	//渠道商
	private static int sellerId = AgentUsers.rootUserId;
	private static String sellerName = "NickautoTest";
	//渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家采购价格列表(ByCatalogId)Test");
	}

	@Before
	public void caseUp() {
		getmchcatalogpricelistBean=new GetMchCatalogPriceListBean();
		getmchcatalogpricelistCall=new GetMchCatalogPriceListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getmchcatalogpricelistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchCatalogPriceList_001() {
		Logger.start(true,"普通代理查询采购价格，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,true);
			
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,sellerId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			List<String> catalogIds = new ArrayList<>();
			ymtCatalogsIWapper.selectCatalogsByProductId(productId).forEach(p->{
				catalogIds.add(p.getsCatalogId());
			});
			
			getmchcatalogpricelistBean.setMchId(agentUserId1);
			getmchcatalogpricelistBean.setCatalogIdList(catalogIds);
			getmchcatalogpricelistCall.setData(getmchcatalogpricelistBean);
			getmchcatalogpricelistCall.callService();			
			Logger.verifyEquals("200", getmchcatalogpricelistCall.getString("code"), "验证code");
			JsonArray data = getmchcatalogpricelistCall.getJsonObject().getAsJsonArray("data");
			Logger.verifyEquals(productId, data.get(0).getAsJsonObject().get("productId"), "验证productid");
			Logger.verifyEquals(productId, data.get(1).getAsJsonObject().get("productId"), "验证productid");
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchCatalogPriceList_002() {
		Logger.start(true,"渠道商查询采购价格，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogIds = new ArrayList<>();
			ymtCatalogsIWapper.selectCatalogsByProductId(productId).forEach(p->{
				catalogIds.add(p.getsCatalogId());
			});
			getmchcatalogpricelistBean.setMchId(sellerId);
			getmchcatalogpricelistBean.setCatalogIdList(catalogIds);
			getmchcatalogpricelistCall.setData(getmchcatalogpricelistBean);
			getmchcatalogpricelistCall.callService();			
			Logger.verifyEquals("300", getmchcatalogpricelistCall.getString("code"), "验证code");
			Logger.verifyIsContains("无上级", getmchcatalogpricelistCall.getString("message"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchCatalogPriceList_003() {
		Logger.start(true,"商户id错误，查询失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogIds = new ArrayList<>();
			ymtCatalogsIWapper.selectCatalogsByProductId(productId).forEach(p->{
				catalogIds.add(p.getsCatalogId());
			});
			getmchcatalogpricelistBean.setMchId(1123);
			getmchcatalogpricelistBean.setCatalogIdList(catalogIds);
			getmchcatalogpricelistCall.setData(getmchcatalogpricelistBean);
			getmchcatalogpricelistCall.callService();			
			Logger.verifyEquals("300", getmchcatalogpricelistCall.getString("code"), "验证code");
			Logger.verifyIsContains("无上级", getmchcatalogpricelistCall.getString("message"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchCatalogPriceList_004() {
		Logger.start(true,"代理商规格id不属于自己，失败");
		try {
			getmchcatalogpricelistBean.setMchId(agentUserId1);
			getmchcatalogpricelistBean.setCatalogIdList(Arrays.asList("c5852990","c5852989"));
			getmchcatalogpricelistCall.setData(getmchcatalogpricelistBean);
			getmchcatalogpricelistCall.callService();			
			Logger.verifyEquals("300", getmchcatalogpricelistCall.getString("code"), "验证code");
			Logger.verifyIsContains("无法找到规格", getmchcatalogpricelistCall.getString("message"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}